var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : "/zofui_sales/cityinfo/index",
        page : {
            isend : false,
            doing : false,
            doo : 'cityinfo',
            pdata : {
                op : 'index',
                page : 1,
                gtype : 1,
                otype : 1,
                sortid : 0,
                search : '',
                issort : 1,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        allsort : [],
        alltopic : [],
        color : '',
        option1: [
            { text: '选择类型', value: 0 },
            { text: '新款商品', value: 1 },
            { text: '活动商品', value: 2 }
        ],
        option2: [
            { text: '选择话题', value: 0 },
            { text: '新款商品', value: 1 },
            { text: '活动商品', value: 2 }
        ],
        option3: [
            { text: '距离最近', value: 0 },
            { text: '最新发布', value: 1 },
            { text: '热门推荐', value: 2 },
            { text: '我发布的', value: 3 },
            { text: '我评论的', value: 4 },
            { text: '我喜欢的', value: 5 },
        ],
        option4: [
            { text: '选择区域', value: 0 },
        ],
        adlist : {},
    },
    onUnload(){
       this.selectComponent('#infoitemid').stoppay();
    },
    onHide(){
       this.selectComponent('#infoitemid').stoppay();
    },
    onLoad:function(options){
        var self = this;
		
		var shopid = options.shopid ? options.shopid : '';
        
        var zfid = options.zfid ? options.zfid : '';
        app.com.getLocation(self,function(loc){
            self.setData({
                'page.pdata.lat' : loc.latitude,
                'page.pdata.lng' : loc.longitude,
                'page.pdata.page' : 1,
                'page.pdata.issort' : 1,
                options : options,
                'page.pdata.zfid' : zfid,
                'page.isend' : false,
                'page.doing' : false,
            });
            app.com.auth(function(user){
                app.com.getPage(self,self.data.page,0,function(res){
                    var data = res.data.data;
                    var color = app.com.mainColor(data.params.style);

                    self.setData({
                        datalist : data.list,
                        params : data.params,
                        allsort : data.allsort ? data.allsort : [],
                        option1 : data.option1,
                        option2 : data.option2,
                        option4 : data.citylist ? data.citylist : [],
                        alltopic : data.alltopic ? data.alltopic : [],
                        adlist : data.adlist ? data.adlist : {},
                        sharedata : data.sharedata,
                        inited : true,
                        'page.pdata.isinit' : 0,
                        'page.pdata.issort' : 0,
                        color : color,
                    });
                    
                    wx.setNavigationBarColor({
                        frontColor: data.params.color == '#FFFFFF' ? '#000000' : '#ffffff',
                        backgroundColor: data.params.color ? data.params.color : color,
                    });

                    wx.setNavigationBarTitle({
                        title:  res.data.data.params.title ? res.data.data.params.title : '信息',
                    });

                },false,function(res){
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(0);
                    });
                });
				app.com.setBar(app,self,shopid);
            });
        });
    },
    onShareAppMessage : function(e){
        var self = this;
        if( e.target && e.target.dataset.c ){
            return {
                title: self.data.datalist[e.target.dataset.i].content,
                path: '/zofui_sales/cityinfo/info?id='+self.data.datalist[e.target.dataset.i].id,
                imageUrl : '',
            }
        }else{
            return {
                title: self.data.sharedata.title,
                path: self.data.sharedata.path,
                imageUrl : '',
            }
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad( this.data.options );
        this.data.isdown = false;

        wx.stopPullDownRefresh();
    },
    onSearch(e){
        var self = this;
        self.setData({
            datalist : [],
            'page.pdata.search' : e.detail,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    clearsearch(){
        var e = {
            detail : '',
        }
        this.searchfn(e);
    },
    showselect(){
        this.setData({
            showselect : !this.data.showselect
        })
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    changetype(e){
        var self = this;
        var type = e.currentTarget.dataset.type*1;
        var value = e.detail*1;
        if(type == 1){
            var keys = 'sid';
        }else if(type == 2){
            var keys = 'tid';
        }else if(type == 3){
            var keys = 'otype';
        }else if(type == 4){
            var keys = 'cityid';
        }

        self.setData({
            datalist : [],
            ['page.pdata.'+keys] : value,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
        });
    },
    onclear(){
        var e = {
            detail : '',
        }
        this.onSearch(e);
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },

})